package advert_api

import (
	"github.com/fatih/structs"
	"github.com/gin-gonic/gin"
	"gvb_server/global"
	"gvb_server/models"
	"gvb_server/models/res"
	"strconv"
)

func (AdvertApi) AdvertUpdate(c *gin.Context) {
	var ar AdvertRequest
	id := c.Param("id")
	inInt, _ := strconv.Atoi(id)
	idUint := uint(inInt)
	err := c.ShouldBindJSON(&ar)
	if err != nil {
		res.FailWithError(err, &ar, c)
		return
	}
	maps := structs.Map(&ar)
	err = global.DB.Model(&models.AdvertModel{Model: models.Model{ID: idUint}}).Updates(maps).Error
	if err != nil {
		global.Log.Error(err)
		res.Fail("更新广告失败！", c)
	}
	res.OkWith(c)
}
